﻿@model FunPlus.Application.Marketing.Dto.GetSubsidyOutput

@{
    ViewBag.Title = "添加补贴";
}
@section CSS{
    <link href="~/Content/css/layerui/layui.css" rel="stylesheet" />
}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <section class="content-header">
        <h1>
            @ViewBag.text
            <small>补贴</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="#">营销管理</a></li>
            <li><a href="/Marketing/Subsidies">补贴列表</a></li>
            <li class="active">@ViewBag.text 补贴</li>
        </ol>
    </section>
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <!-- Custom Tabs -->
                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab">基本信息</a></li>
                        <li><a href="#tab_2" data-toggle="tab">补贴规则</a></li>
                        <li class="pull-right">
                            <a class="btn btn-app" onclick="OpenSubsidyItem('添加补贴规则','/Marketing/SubsidyItem?subsidy_id=@Model.subsidy_id&id=0',800,500)" href="javascript:void(0);">
                                <i class="fa fa-plus-square"></i> 添加规则
                            </a>
                        </li>
                    </ul>
                    <form class="form-horizontal" id="from-add" novalidate="novalidate" method="post" action="/Marketing/Subsidy">

                        <div class="tab-content">
                            <div class="tab-pane active" id="tab_1">
                                <div class="ibox float-e-margins">
                                    <div class="ibox-content">
                                        @Html.AntiForgeryToken()
                                        @Html.HiddenFor(model => model.subsidy_id)
                                        <div class="form-horizontal">
                                            <div class="form-group">
                                                @Html.LabelFor(model => model.subsidy_name, htmlAttributes: new { @class = "control-label col-md-2" })
                                                <div class="col-md-9">
                                                    @Html.EditorFor(model => model.subsidy_name, new { htmlAttributes = new { @class = "form-control" } })
                                                    @Html.ValidationMessageFor(model => model.subsidy_name, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                @Html.LabelFor(model => model.remark, htmlAttributes: new { @class = "control-label col-md-2" })
                                                <div class="col-md-9">
                                                    @Html.EditorFor(model => model.remark, new { htmlAttributes = new { @class = "form-control" } })
                                                    @Html.ValidationMessageFor(model => model.remark, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="control-label col-md-2">发行金额</label>
                                                <div class="col-md-9">
                                                    <input type="text" class="form-control" id="amount" name="amount" value="@Model.amount" placeholder="限制使用额度" />
                                                    @Html.ValidationMessageFor(model => model.amount, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="control-label col-md-2">有效期</label>
                                                <div class="col-md-9">
                                                    <div class="col-md-2" style="padding-left:0px">
                                                        <input type="text" class="form-control" id="start_time" name="start_time" value="@Model.start_time" placeholder="开始时间" />
                                                        @Html.ValidationMessageFor(model => model.start_time, "", new { @class = "text-danger" })
                                                    </div>
                                                    <div class="col-md-1" style="padding:5px; width:40px">
                                                        <label class="form-label">至</label>
                                                    </div>
                                                    <div class="col-md-2" style="padding-left:0px">
                                                        <input type="text" class="form-control" id="end_time" name="end_time" value="@Model.end_time" placeholder="结束时间" />
                                                        @Html.ValidationMessageFor(model => model.end_time, "", new { @class = "text-danger" })
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="control-label col-md-2">适用商品</label>
                                                <div class="col-md-10">
                                                    <button type="button" class="btn btn-info" id="btnAdd" onclick="OpenGoods('添加商品','/Marketing/GetGoods',1200,600)">
                                                        <i class="fa fa-plus"></i> 添加商品
                                                    </button>
                                                    <br />
                                                    <br />
                                                    @Html.EditorFor(model => model.goods_ids, new { htmlAttributes = new { @style = "width:330px;height:100px;" } })
                                                    @Html.ValidationMessageFor(model => model.goods_ids, "", new { @class = "text-danger" })

                                                </div>
                                            </div>

                                            <div class="form-group">
                                                @Html.LabelFor(model => model.status, htmlAttributes: new { @class = "control-label col-md-2" })
                                                <div class="col-md-10">
                                                    <select class="form-control" id="status" name="status">
                                                        <option value="1">启用</option>
                                                        <option value="0">禁用</option>
                                                    </select>
                                                    @Html.ValidationMessageFor(model => model.status, "", new { @class = "text-danger" })
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <div class="col-sm-4 col-sm-offset-3">
                                                    <button class="btn btn-primary" type="submit">保存</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- /.tab-pane -->
                            <div class="tab-pane" id="tab_2">
                                <table class="table table-hover">
                                    <tbody>
                                        <tr>
                                            <th>编号</th>
                                            <th>标题</th>
                                            <th>费率</th>
                                            <th>总周期</th>
                                            <th>备注</th>
                                            <th>添加时间</th>
                                        </tr>
                                        @if (Model.subsidy_items != null)
                                        {
                                            for (var i = 0; i < Model.subsidy_items.Count; i++)
                                            {
                                                <tr>
                                                    <td>@Html.DisplayTextFor(a => a.subsidy_items[i].id) @Html.HiddenFor(a => a.subsidy_items[i].id)</td>
                                                    <td>@Html.TextBoxFor(a => a.subsidy_items[i].name, new { @class = "form-control" })</td>
                                                    <td>@Html.TextBoxFor(a => a.subsidy_items[i].rate, new { @class = "form-control" })</td>
                                                    <td>@Html.TextBoxFor(a => a.subsidy_items[i].total_cycle, new { @class = "form-control" })</td>
                                                    <td>@Html.TextBoxFor(a => a.subsidy_items[i].remark, new { @class = "form-control" })</td>
                                                    <td>@Html.TextBoxFor(a => a.subsidy_items[i].add_time, new { @class = "form-control" })</td>
                                                </tr>
                                            }
                                        }
                                    </tbody>
                                </table>

                                <div class="form-group">
                                    <div class="col-sm-4 col-sm-offset-3">
                                        <button class="btn btn-primary" type="submit">保存</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                    <!-- /.tab-content -->
                </div>
                <!-- nav-tabs-custom -->
            </div>
        </div>
    </section>
</div>

@section JS{
    <script src="~/Content/js/jquery.form.js"></script>
    <script src="~/Content/plugins/validate/jquery.validate.min.js"></script>
    <script src="~/Content/plugins/validate/messages_zh.min.js"></script>
    <script type="text/javascript">
        var subsidy_id = getURLParameter("subsidy_id");

        $(function () {
            $("#status").val(@Model.status);
        });

        $('#start_time').datepicker({
            autoclose: true
        });

        $('#end_time').datepicker({
            autoclose: true
        });

        function OpenSubsidyItem(title, url, w, h) {
            if (subsidy_id==0) {
                layer.msg("请先添加基本信息", {
                    icon: 2,
                    time: 1500
                });
                return;
            }

            layer_show2(title, url, w, h);
        }

        function OpenGoods(title, url, w, h){
            var goods_ids=$('#goods_ids').val();
            if(goods_ids.length>0){
                url= url+"?goods_ids="+goods_ids;
            }

            layer_show(title, url, w, h);
        }

        /*弹出层*/
        /*
	        参数解释：
	        title	标题
	        url		请求的url
	        id		需要操作的数据id
	        w		弹出层宽度（缺省调默认值）
	        h		弹出层高度（缺省调默认值）
        */
        function layer_show(title, url, w, h) {
            if (title == null || title == '') {
                title = false;
            };
            if (url == null || url == '') {
                url = "404.html";
            };
            if (w == null || w == '') {
                w = 800;
            };
            if (h == null || h == '') {
                h = ($(window).height() - 50);
            };

            layer.open({
                    type: 2,
                    title: title,
                    fixed: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade: 0.4,
                    area: [w + 'px', h + 'px'],
                    content: url,
                    btn: ['确定','关闭'],
                    yes: function(index,layero){
                        //当点击‘确定’按钮的时候，获取弹出层返回的值
                        var res= $(layero).find("iframe")[0].contentWindow.callbackdata();
                        //打印返回的值，看是否有我们想返回的值。
                        $('#goods_ids').val(res);
                        //最后关闭弹出层
                        layer.close(index);
                    },
                    cancel: function(index){
                        layer.close(index);
                        //右上角关闭回调
                    }
                });

        }

         /*弹出层*/
        /*
	        参数解释：
	        title	标题
	        url		请求的url
	        id		需要操作的数据id
	        w		弹出层宽度（缺省调默认值）
	        h		弹出层高度（缺省调默认值）
        */
        function layer_show2(title, url, w, h) {
            if (title == null || title == '') {
                title = false;
            };
            if (url == null || url == '') {
                url = "404.html";
            };
            if (w == null || w == '') {
                w = 800;
            };
            if (h == null || h == '') {
                h = ($(window).height() - 50);
            };

            layer.open({
                    type: 2,
                    title: title,
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade: 0.8,
                    area: [w + 'px', h + 'px'],
                    content: url
                });

        }



    </script>
}